<template>
  <div>
    <page>
      <div slot="body">
        <div>
          <div class="line"></div>
          <el-form class="form" label-width="140px" inline>
            <!-- <el-form-item class="form-item" label="设计总需求量：" prop="name">
              <el-input v-model="total" placeholder="" disabled size="small" />
              <i class="el-icon-edit" @click="edit(1)"></i>
            </el-form-item>
            <el-form-item class="form-item" label="设计完成时间：" prop="name">
              <el-input v-model="time" placeholder="" disabled size="small" />
              <i class="el-icon-edit" @click="edit(2)"></i>
            </el-form-item> -->
          </el-form>
        </div>
        <div class="table-operation">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd"> 添加维保记录 </el-button>
        </div>
        <el-table :data="tableData" border>
          <el-table-column label="序号" type="index" width="50px"></el-table-column>
          <el-table-column label="产线" prop="line">
            <template slot-scope="scope">
              <span>{{
                (dicts1.find(item => item.dictValue == scope.row.line) || {}).dictLabel ||
                scope.row.line
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="维修保养时间" prop="maintenanceTime"></el-table-column>
          <el-table-column label="维修保养内容" prop="maintenanceContent" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作" width="100px">
            <template slot-scope="scope">
              <el-link type="primary" @click="handleEdit(scope.row)" :underline="false">编辑</el-link>
              <el-link type="primary" @click="handleDelete(scope.row)" :underline="false">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog :title="title1" :close-on-click-modal="false" :visible.sync="open1" width="800px" append-to-body>
        <el-form ref="Form" :model="Form" :rules="Rules" label-width="140px">
          <el-form-item label="产线：" prop="line">
            <el-select placeholder="请选择产线" v-model="Form.line" class="query-item">
              <el-option
                v-for="item in dicts1"
                :key="item.id"
                :label="item.dictLabel"
                :value="item.dictValue"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="预警类型：" prop="warnType">
            <el-select placeholder="请选择预警类型" v-model="Form.warnType" class="query-item">
              <el-option
                v-for="item in dicts2"
                :key="item.id"
                :label="item.dictLabel"
                :value="item.dictValue"
              >
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="维修保养时间：" prop="maintenanceTime">
            <el-date-picker
              v-model="Form.maintenanceTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="维修保养内容：" prop="maintenanceContent">
            <el-input type="textarea" v-model="Form.maintenanceContent" placeholder="请输入维修保养内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <div slot="footer">
        <el-pagination
          background
          :page-size="pageVO.pageSize"
          :current-page.sync="pageVO.pageNum"
          :total="pageVO.total"
          :layout="pageOptions.layout"
          :page-sizes="pageOptions.pageSizes"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </page>
  </div>
</template>

<script>
import { getList, edit, deleteById } from '@/api/maintenanceRecord';
import { getDicts } from '@/api/records'
import { getToken } from '@/utils/auth';
import pageQuery from '../../mixins/pageQuery';
export default {
  mixins: [pageQuery],
  data() {
    return {
      dicts1: [],
      dicts2: [],
      form: {
        id: '',
      },
      Form: {},
      open1: false,
      total: '',
      time: '',
      title1: '',
      tableData: [],
      num: 0,
      Rules: {
        maintenanceTime: [{ required: true, message: '请输入时间', trigger: 'blur' }],
        line: [{ required: true, message: '请输入产线', trigger: 'blur' }],
      },
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/steel-arch/client/file/upload', // 上传的图片服务器地址
      headers: {
        Authorization: 'Bearer ' + getToken(),
      },
      fileList: [],
    };
  },
  created() {
    this.getTableData();
    const { projectId } = this.$store.getters;
    this.Form.projectId = projectId;
    getDicts({ dictType: 'device_line' }).then(res => {
      this.dicts1 = res.data.data;
    });
    getDicts({ dictType: 'warn_type' }).then(res => {
      this.dicts2 = res.data.data;
    });
  },
  methods: {
    getTableData() {
      const { projectId } = this.$store.getters;
      const parmas = {
        projectId: projectId,
      };
      getList(parmas).then(res => {
        this.tableData = res.data.records;
        this.pageVO.total = res.data.total
      });
    },
    submitForm: function () {
      const { projectId } = this.$store.getters;
      this.$refs['Form'].validate(valid => {
        if (valid) {
          if (this.Form.id != null) {
            edit(this.Form).then(response => {
              this.$message.success('修改成功');
              this.open1 = false;
              this.getTableData();
            });
          } else {
            this.Form.projectId = projectId;
            edit(this.Form).then(response => {
              this.$message.success('新增成功');
              this.open1 = false;
              this.getTableData();
            });
          }
        }
      });
    },
    cancel() {
      this.$refs.Form.resetFields();
      this.open1 = false;
    },
    handleAdd() {
      this.open1 = true;
      this.title1 = '添加产品';
      this.Form={}
    },
    handleEdit(row) {
      this.open1 = true;
      this.Form = JSON.parse(JSON.stringify(row));
      const { projectId } = this.$store.getters;
      this.Form.projectId = projectId;
    },
    handleDelete(row) {
      this.$confirm('是否确认删除产线为"' + row.line + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return deleteById(row.id);
        })
        .then(() => {
          this.getTableData();
          this.$message.success('删除成功');
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  justify-content: flex-start;
  margin: 20px 0;
}

.line {
  width: 8px;
  background: #1890ff;
  margin-right: 15px;
}
.form {
  display: flex;
  margin-top: 10px;
  ::v-deep {
    .el-form-item__label {
      font-weight: normal;
    }
  }
}
.form-item {
  position: relative;
}
.el-icon-edit {
  font-size: 20px;
  right: 10px;
  position: absolute;
  top: 8px;
}
</style>
